const $api = require('../../utils/api').API
const $cache = require('../../utils/storage')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailInfo: null,
    isLike: false,
    comment: '', //评论表单
    openId: "",
    isMine:false
  },
  handleSend() {
    let that = this
    $api.detail.addComment({
      "commenter": this.data.openId,
      "momentId": this.data.detailInfo.id,
      "content": this.data.comment
    }).then(res=>{
        console.log('评论成功')
        that.setData({comment:''})
        that.onLoad({id:that.data.detailInfo.id,openId:that.data.openId})
    })
  },
  handleInput(e) {
    console.log(e.detail.value)
    this.setData({
      comment: e.detail.value
    })
  },
  handleBack() {
    wx.switchTab({
      url:'/pages/home_new/home_new'
    })
  },
  handleLike() {
    if (!this.data.isLike) {
      $api.addLike({
        momentId: this.data.detailInfo.momentsId,
        openId: this.data.openId
      })
    } else {
      $api.deleteLike({
        momentId: this.data.detailInfo.momentsId,
        openId: this.data.openId
      })
    }
    this.setData({
      isLike: !this.data.isLike
    })
    this.onLoad({id:this.data.detailInfo.id,openId:this.data.openId})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    let {
      id,
      openId
    } = options
    let myOpenId = $cache._getStorage('openId')

    $api.detail.getDetail({
      id,
      openId:myOpenId
    }).then(res => {
      this.setData({
        detailInfo: res.data.data,
        isLike: res.data.data.hasLike,
        openId: $cache._getStorage('openId'),
        isMine: openId==myOpenId
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})